@echo off
setlocal enabledelayedexpansion

REM MongoShake 定时任务管理脚本 (Windows版本)

set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "NC=[0m"

REM 打印带颜色的消息
:print_message
echo %GREEN%[INFO]%NC% %~1
goto :eof

:print_warning
echo %YELLOW%[WARNING]%NC% %~1
goto :eof

:print_error
echo %RED%[ERROR]%NC% %~1
goto :eof

REM 检查容器状态
:check_container
docker ps | findstr "mongoshake-sync" >nul
if errorlevel 1 (
    call :print_error "MongoShake容器未运行，请先启动容器"
    exit /b 1
)
goto :eof

REM 查看定时任务状态
:show_cron_status
call :print_message "查看定时任务状态..."
docker exec mongoshake-sync crontab -l
goto :eof

REM 查看定时任务日志
:show_cron_logs
call :print_message "查看定时任务日志..."
if "%1"=="health" (
    docker exec mongoshake-sync tail -f /var/log/mongoshake/health_check.log
) else (
    docker exec mongoshake-sync tail -f /var/log/mongoshake/cron.log
)
goto :eof

REM 手动执行一次同步
:run_sync_now
call :print_message "手动执行一次同步..."
docker exec mongoshake-sync python3 /app/container_scheduler.py --once
if errorlevel 1 (
    call :print_error "同步执行失败"
) else (
    call :print_message "同步执行成功"
)
goto :eof

REM 重启定时任务
:restart_cron
call :print_message "重启定时任务服务..."
docker exec mongoshake-sync pkill cron
timeout /t 2 /nobreak >nul
docker exec mongoshake-sync cron
call :print_message "定时任务服务已重启"
goto :eof

REM 查看调度器日志
:show_scheduler_logs
call :print_message "查看调度器日志..."
docker exec mongoshake-sync tail -f /var/log/mongoshake/container_scheduler.log
goto :eof

REM 查看所有日志
:show_all_logs
call :print_message "查看所有相关日志..."
echo.
echo === 定时任务日志 ===
docker exec mongoshake-sync tail -20 /var/log/mongoshake/cron.log
echo.
echo === 健康检查日志 ===
docker exec mongoshake-sync tail -20 /var/log/mongoshake/health_check.log
echo.
echo === 调度器日志 ===
docker exec mongoshake-sync tail -20 /var/log/mongoshake/container_scheduler.log
echo.
echo === MongoShake日志 ===
docker exec mongoshake-sync tail -20 /var/log/mongoshake/mongoshake.log
goto :eof

REM 清理日志
:clean_logs
call :print_warning "清理所有日志文件..."
docker exec mongoshake-sync find /var/log/mongoshake -name "*.log" -delete
call :print_message "日志文件已清理"
goto :eof

REM 显示帮助信息
:show_help
echo Usage: %0 [COMMAND]
echo.
echo Commands:
echo   status      Show cron job status
echo   logs        Show cron logs (cron^|health^|scheduler^|all)
echo   run         Run sync manually
echo   restart     Restart cron service
echo   clean       Clean all log files
echo   help        Show this help message
echo.
echo Examples:
echo   %0 status                   # Show cron job status
echo   %0 logs cron                # Show cron logs
echo   %0 logs health              # Show health check logs
echo   %0 logs scheduler           # Show scheduler logs
echo   %0 logs all                 # Show all logs
echo   %0 run                      # Run sync manually
echo   %0 restart                  # Restart cron service
echo   %0 clean                    # Clean all logs
goto :eof

REM 主函数
:main
call :check_container
if errorlevel 1 exit /b 1

if "%1"=="" goto :show_help
if "%1"=="status" goto :show_cron_status
if "%1"=="logs" goto :show_cron_logs
if "%1"=="run" goto :run_sync_now
if "%1"=="restart" goto :restart_cron
if "%1"=="clean" goto :clean_logs
if "%1"=="help" goto :show_help
if "%1"=="--help" goto :show_help
if "%1"=="-h" goto :show_help

call :print_error "Unknown command: %1"
call :show_help
exit /b 1

:show_cron_logs
call :show_cron_logs %2
goto :eof

REM 执行主函数
call :main %* 